package com.blueocean_health.care.management.transfer.server;

import cn.jpush.api.report.UsersResult;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.SelectCareSalaryDto;
import com.blueocean_health.care.management.domain.dto.SelectCareSalaryRespDto;
import com.blueocean_health.care.management.domain.dto.SelectCareSalarySummaryDetailRespDto;
import com.blueocean_health.care.management.domain.dto.SelectCareSalarySummaryRespDto;
import com.blueocean_health.care.management.excel.ExcelUtil;
import com.blueocean_health.care.management.exception.OrderServiceException;
import com.blueocean_health.care.management.service.SelectCareSalaryService;
import com.blueocean_health.care.management.util.TimeType;
import com.blueocean_health.care.management.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 护工工资报表
 * @author cp
 */
@RestController
@RequestMapping(value = "wagesReport")
public class SelectCareSalaryController {
	
	@Autowired
	private SelectCareSalaryService selectCareSalaryService;
	
	/**
	 * 护工工资报表
	 * @param careSalaryDto
	 * @return
	 * @throws OrderServiceException
	 */
	@RequestMapping(value = "report")
	public SysResult queryNurseSalary(@RequestBody SelectCareSalaryDto careSalaryDto) throws OrderServiceException {
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
		return selectCareSalaryService.queryCareSalaryReport(careSalaryDto);
	}


	/**
	 * 提成汇总表
	 * @param careSalaryDto
	 * @return
	 * @throws OrderServiceException
	 */
	@RequestMapping(value = "summary")
	public SysResult queryNurseSalarySummary(@RequestBody SelectCareSalaryDto careSalaryDto) throws OrderServiceException {
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
		return selectCareSalaryService.queryNurseSalarySummary(careSalaryDto);
	}


	/**
	 * 提成汇总表详情
	 * @param careSalaryDto
	 * @return
	 * @throws OrderServiceException
	 */
	@RequestMapping(value = "summary/detail")
	public SysResult queryNurseSalarySummaryDetail(@RequestBody SelectCareSalaryDto careSalaryDto) throws OrderServiceException {
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
		careSalaryDto.setPatientKey("");
		careSalaryDto.setOutWorkerKey("");
		careSalaryDto.setOrderId("");
		return selectCareSalaryService.queryCareSalaryReport(careSalaryDto);
	}


	/**
	 * 导出Excel
	 * @param careSalaryDto
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "report/excel")
	public List<SelectCareSalaryRespDto> queryNurseSalaryExcel(@RequestBody SelectCareSalaryDto careSalaryDto, HttpServletResponse response) throws Exception{
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
		List<SelectCareSalaryRespDto> list = selectCareSalaryService.queryCareSalaryReportExcel(careSalaryDto);
//		if(list == null || list.size() == 0) return;

		return  list;
		/*String finalFileName = null;
		try {
			finalFileName = URLEncoder.encode("提成明细表_"+ TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
		} catch (Exception e) {
			finalFileName  = "care_salary_report_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
		}
		response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		
		ExcelUtil.getInstance().exportObj2Excel(toClient, list, SelectCareSalaryRespDto.class);*/
	}



    /**
     * 提成汇总表导出Excel
     * @param careSalaryDto
     * @return
     * @throws OrderServiceException
     */
    @RequestMapping(value = "summary/excel")
    public List<SelectCareSalarySummaryRespDto> queryNurseSalarySummaryExcel(@RequestBody SelectCareSalaryDto careSalaryDto, HttpServletResponse response) throws Exception{
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
        List<SelectCareSalarySummaryRespDto> respDtos = selectCareSalaryService.queryNurseSalarySummaryExcel(careSalaryDto);

        return  respDtos;
      /*  String finalFileName = null;
        try {
            finalFileName = URLEncoder.encode("提成汇总表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "care_salary_summary_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

        ExcelUtil.getInstance().exportObj2Excel(toClient, respDtos, SelectCareSalarySummaryRespDto.class);*/
    }


    /**
     * 提成汇总明细导出Excel
     * @param careSalaryDto
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "summary/detail/excel")
    public List<SelectCareSalarySummaryDetailRespDto> queryNurseSummaryDetailExcel(@RequestBody SelectCareSalaryDto careSalaryDto, HttpServletResponse response) throws Exception{
		careSalaryDto.setHospitalId(careSalaryDto.getTeamId());
        careSalaryDto.setOutWorkerKey("");
        careSalaryDto.setPatientKey("");
        List<SelectCareSalaryRespDto> list = selectCareSalaryService.queryCareSalaryReportExcel(careSalaryDto);
        List<SelectCareSalarySummaryDetailRespDto> datalist = new ArrayList<>();
        list.forEach(dto ->{
			SelectCareSalarySummaryDetailRespDto respDto = new SelectCareSalarySummaryDetailRespDto();
			respDto.setDayPay(dto.getDayPay());
			respDto.setOrderId(dto.getOrderId());
			respDto.setOutworkerName(dto.getOutworkerName());
			respDto.setPatientName(dto.getPatientName());
			respDto.setWorkingDay(dto.getWorkingDay());
			respDto.setWorkName(dto.getWorkName());
			respDto.setDeptName(dto.getDeptName());
			respDto.setReceiptNumber(dto.getReceiptNumber());
			respDto.setOrderTypeName(dto.getOrderTypeName());
			datalist.add(respDto);
		});

        return  datalist;

       /* String finalFileName = null;
        try {
            finalFileName = URLEncoder.encode("汇总详情表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "care_salary_summarydetail_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

        ExcelUtil.getInstance().exportObj2Excel(toClient, datalist, SelectCareSalarySummaryDetailRespDto.class);*/
    }
	
	
	
}
